package com.iflytek.exam.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.iflytek.common.core.domain.entity.SysUser;
import com.iflytek.exam.domain.CreateExam;
import com.iflytek.exam.domain.ExamSet;
import com.iflytek.exam.dto.CheckExamDTO;
import com.iflytek.exam.service.ICreateExamService;
import com.iflytek.exam.service.IExamSetService;
import com.iflytek.exam.vo.ShortAnswerSubmitVo;
import com.iflytek.system.service.ISysUserService;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.iflytek.common.annotation.Log;
import com.iflytek.common.core.controller.BaseController;
import com.iflytek.common.core.domain.AjaxResult;
import com.iflytek.common.enums.BusinessType;
import com.iflytek.exam.domain.CheckExam;
import com.iflytek.exam.service.ICheckExamService;
import com.iflytek.common.utils.poi.ExcelUtil;
import com.iflytek.common.core.page.TableDataInfo;

/**
 * 批改试卷Controller
 *
 * @author KK
 * @date 2024-07-09
 */
@RestController
@RequestMapping("/exam/checkExam")
public class CheckExamController extends BaseController {
    @Autowired
    private ICheckExamService checkExamService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ICreateExamService examService;
    @Autowired
    private IExamSetService examSetService;

    /**
     * 查询批改试卷列表
     */
    @PreAuthorize("@ss.hasPermi('exam:checkExam:list')")
    @GetMapping("/list")
    public TableDataInfo list(CheckExam checkExam) {
        startPage();
        List<CheckExam> list = checkExamService.selectCheckExamList(checkExam);
        List<CheckExamDTO> dtoList = new ArrayList<>();
        list.forEach(examGrade -> {
            CheckExamDTO checkExamDTO = new CheckExamDTO();
            SysUser sysUser = sysUserService.selectUserById(examGrade.getUserId());
            CreateExam createExam = examService.selectcreateExamById(examGrade.getExamId());
            BeanUtils.copyProperties(examGrade, checkExamDTO);
            checkExamDTO.setUsername(sysUser.getUserName());
            if (createExam != null){
                checkExamDTO.setExamName(createExam.getName());
            }
            dtoList.add(checkExamDTO);
        });
        return getDataTable(dtoList);
    }


    /**
     *
     * 返回学生提交答案之后查看试卷详情
     */
    @PreAuthorize("@ss.hasPermi('exam:checkExam:look')")
    @GetMapping("/look/{examId}")
    public AjaxResult selectBasicInfo(@PathVariable("examId") Long examId) {
        ExamSet examSet = examSetService.selectExamSetByExamId(examId);
        if (examSet.getIsLook() == 0){
            return AjaxResult .warn("考试完不允许查看!");
        }
        return AjaxResult.success(checkExamService.selectBasicInfo(examId));
    }

    /**
     *
     * 老师查看学生提交答案之后试卷详情（批改试卷）
     */
    @PreAuthorize("@ss.hasPermi('exam:checkExam:correct')")
    @GetMapping("/check/{gradeId}")
    public AjaxResult checkBasicInfo(@PathVariable("gradeId") Long gradeId) {
        return AjaxResult.success(checkExamService.checkBasicInfo(gradeId));
    }


    /**
     *
     * 查询老师创建的试卷中试题详情
     */
    @PreAuthorize("@ss.hasPermi('exam:checkExam:review')")
    @GetMapping("/review/{examId}")
    public AjaxResult reviewBasicInfo(@PathVariable("examId") Long examId) {
        return AjaxResult.success(checkExamService.reviewBasicInfo(examId));
    }

    /**
     * 批改简答题，更新分数
     */
    @PreAuthorize("@ss.hasPermi('exam:checkExam:update')")
    @GetMapping("/update")
    public AjaxResult correctExam(@RequestBody List<ShortAnswerSubmitVo> shortAnswerSubmitVos) {
        return toAjax(checkExamService.updateScore(shortAnswerSubmitVos));
    }

    /**
     * 导出批改试卷列表
     */
    @PreAuthorize("@ss.hasPermi('exam:checkExam:export')")
    @Log(title = "批改试卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheckExam checkExam) {
        List<CheckExam> list = checkExamService.selectCheckExamList(checkExam);
        ExcelUtil<CheckExam> util = new ExcelUtil<CheckExam>(CheckExam.class);
        util.exportExcel(response, list, "批改试卷数据");
    }

    /**
     * 获取批改试卷详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:checkExam:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(checkExamService.selectCheckExamById(id));
    }

    /**
     * 新增批改试卷
     */
    @PreAuthorize("@ss.hasPermi('exam:checkExam:add')")
    @Log(title = "批改试卷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheckExam checkExam) {
        return toAjax(checkExamService.insertCheckExam(checkExam));
    }

    /**
     * 修改批改试卷
     */
    @PreAuthorize("@ss.hasPermi('exam:checkExam:edit')")
    @Log(title = "批改试卷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheckExam checkExam) {
        return toAjax(checkExamService.updateCheckExam(checkExam));
    }

    /**
     * 删除批改试卷
     */
    @PreAuthorize("@ss.hasPermi('exam:checkExam:remove')")
    @Log(title = "批改试卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(checkExamService.deleteCheckExamByIds(ids));
    }
}
